const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showMoney:true,
    withStatus:1,
    ids:[],
    number:0,
    withTotal:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      let that=this;
      console.log('------mine-------');
      console.log(options);
      console.log('-------mine------');
      var parent_id=options.parent_id;
      if(parent_id!=undefined){
          this.setData({
              parent_id:parent_id
            })
            wx.setStorageSync('parent_id',parent_id);
      }else{
          parent_id = wx.getStorageSync('parent_id');
      
      }
      var open=wx.getStorageSync('open');
      console.log(open);
      var date=util.formatTime(new Date());
      log.info("Time:"+date+",page:mine,options:"+JSON.stringify(options));
      console.log(open)
      log.info("Time:"+date+",page:mine,open:"+JSON.stringify(open));
      if (open === null){
        console.log('open is null')
        wx.navigateTo({
          url: '../login/index?parent_id='+parent_id,
        })
      }
      if(open == ""){
          console.log('open is null')
          wx.navigateTo({
            url: '../login/index?parent_id='+parent_id,
          })
      }else{
          console.log('--mine----')
          console.log(open)
          console.log('----mine---')
      }
 
      that.setData({
        platform:app.globalData.platform,    
        open:open,
         width:app.globalData.windowWidth,
        height:app.globalData.windowHeight,
        statusBarHeight:app.globalData.statusBarHeight,
        navHeight:app.globalData.navHeight,
        marginTop:app.globalData.navTitleTop,
        domain:app.globalData.domain
      })
  },
  goto:function(e){
    console.log(e);
    var url=e.currentTarget.dataset.url;
    var index=e.currentTarget.dataset.index;
    var parent_id=wx.getStorageSync('parent_id');
    if(url=="#"){

    }else if(url=='phone'){
        wx.makePhoneCall({
          phoneNumber: '400-800-2177',
        })
    }else if(index=='index'){
        wx.reLaunch({
          url: url,
        })
    }else{
        wx.navigateTo({
          url: url,
        })
    }
    console.log(url);
},
goBack:function(){
  wx.navigateBack();
},
showModels:function(msg,url){

  wx.showModal({
    cancelColor: 'cancelColor',
    title:'系统提示',
    content:msg,
    complete: (res) => {
      if (res.cancel) {
        
      }
  
      if (res.confirm) {
        if(url!="#"){
            wx.reLaunch({
              url: url,
            })
       }
      }
    }
  })
},
moneyShow:function(){
  var showMoney=this.data.showMoney;
  if(showMoney){
      this.setData({
          showMoney:false
      })
  }else{
      this.setData({
          showMoney:true
      })
  }
},
WxPay(){
  console.log('wxpay')

      var that=this;
      var open=wx.getStorageSync('open');
      wx.request({
        url:app.globalData.domain+'wechat/pay/payment',    
        data: {user_id: open.id,openid: open.mini_openid},
        success: function (res) {
         console.log(res);
         var data=res.data;
          //console.log('调起支付');
          var code=res.data.code;
          console.log(code);
          if(code==200){
          var data=res.data.data;
          console.log(data.timeStamp)
            wx.requestPayment({
                'timeStamp': data.timeStamp,
                'nonceStr': data.nonceStr,
                'package': data.package,
                'signType': 'RSA',
                'paySign': data.paySign,
                'success': function (res) {
                 console.log('success');
                  wx.showToast({
                    title: '支付成功',
                    icon: 'success',
                    duration: 3000
                  });
                  setTimeout(function() {
                      //要延时执行的代码
                      
                    }, 1000) //延迟时间
                  //that.waitPayShow();
                },
                'fail': function (res) {
                  console.log(res);
                },
                'complete': function (res) {
                  console.log('complete');
                }
              });
          }else{
    
          }
      
        },
        fail: function (res) {
          //console.log("fail")
        }
      }); 
    
    
},
goWiths(){
    let that=this;
    var open= wx.getStorageSync('open');
    var data=this.data;
    var total=data.money;
    var idArray=[];
    var moneyArray=data.moneyArray;
    var withStatus=data.withStatus;
    moneyArray.forEach(function(item,index){             
           idArray.push(item.id);              
    })
    var ids=idArray.toString();
    if(total>200){
        wx.showToast({
          title: '提现金额不能大于2000',
        })
    }else if(total<=0){
        wx.showToast({
            title: '提现金额为零',
          })
    }else{
      if(withStatus==1){           
        this.setData({
            withStatus:0
        })
        setTimeout(function () {
         that.setData({
             withStatus: 1
         })
       }, 30000) //阻止一秒点击多次   
        wx.request({
            url: app.globalData.domain+'wechat/with/shareWith',
            data:{user_id:open.id,total:total,ids:ids,from:1},
            success:res=>{
              console.log(res);
              var data=res.data;
              if(data.status==200){
                  that.setData({
                    total:0,
                    number:0
                  })
                wx.showToast({
                  title: '提现成功',
                })
                that.getMoney();
              }else{
                that.setData({
                    total:0,
                    number:0
                })
                that.getMoney();
                that.showModels(data.msg,"#"); 
              }
            }
        })
      }else{
        wx.shouModel({
          title: '30秒内只能点击一次，勿重新点击',
        })
      }
      //  break;
      
    }
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getMoney();
  },
  getMoney(){
    let that=this;
    var open= wx.getStorageSync('open');
    var status=this.data.status;
    wx.request({
      url:app.globalData.domain+'wechat/with/getMoney',    
      data: {user_id: open.id},
         success: function (res) {
           console.log(res.data);
           let data=res.data;
          
           that.setData({
             count:data.count,
             list:data.list,
             money:data.money,
             total:data.total,
             no_with:data.no_with,
             with:data.with,
             moneyArray:data.list

           })
        }
    });
  },

selectWith:function(e){
    console.log(e);       
    var data=this.data;
    var list = data.list;
    var id=e.currentTarget.dataset.id;
    var number=data.number;
    var withTotal=data.withTotal;
    var ids=data.ids
    console.log(id);
    list.forEach(function(item,index){
        var money=item.money;
        console.log(money);
        if(id==item.id){
            console.log(id);
            if(item.select==1){
                item.select=0;
                withTotal=parseFloat(withTotal)-parseFloat(money);
                number=number-1;
                var idt=ids.indexOf(id);
                console.log(idt);
                if (idt > -1) { 
                    ids.splice(idt,1); 
                  }
                  console.log('withTotal='+withTotal);
            }else{
                
                withTotal = parseFloat(withTotal)+parseFloat(money);
                if(withTotal>200){
                    wx.showToast({
                      title: '不能高于200',
                    })
                    
                }else{
                item.select=1;
                console.log('withTotal='+withTotal);
                number=number+1;
                ids.push(id);
               }
            }
            
        }
       
    })
    if(number>0){
       var  status=1;
    }else{
        var status=0
    }
    console.log(ids);
    this.setData({
        withTotal:withTotal,
        number:number,
        list:list,
        ids:ids,
        status:status
    })

},
goWith:function(){
    let that=this;
    var open= wx.getStorageSync('open');
    var data=this.data;
    var withTotal=data.withTotal;
    var ids=data.ids;
    var withStatus=data.withStatus;
    ids=ids.toString();
    if(withTotal>200){
        wx.showToast({
          title: '提现金额不能大于200',
        })
    }else if(withTotal<=0){
        wx.showToast({
            title: '提现金额为零',
          })
    }else{

        if(withStatus==1){           
            this.setData({
                withStatus:0
            })
            setTimeout(function () {
             that.setData({
                 withStatus: 1
             })
           }, 30000) //阻止一秒点击多次   
        wx.request({
            url: app.globalData.domain+'wechat/with/goWith',
            data:{user_id:open.id,total:withTotal,ids:ids,from:1},
            success:res=>{
              console.log(res);
              let datas=res.data;
              if(datas.status==200){
                  that.setData({
                    withTotal:0,
                    number:0,
                    ids:[]
                  })
                wx.showToast({
                  title: '提现成功',
                })
                this.getMoney();
              }else{
                that.setData({
                    withTotal:0,
                    number:0,
                    ids:[]
                  })
           
                  that.showModels(datas.msg,datas.url); 
                  that.getMoney();
              }
            }
        })
    }else{
        wx.showModal({
          title: '重要提示',
          content: '30秒内只能点击一次',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              
            }
          }
        })
    }
    }
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})